// columnMixin.js：专栏管理组件的逻辑混入
import {
  getColumnList,
  getCategoryList,
  getColumnDetail,
  checkColumnStatus,
  toggleColumnStatus,
  statusMap,
  createCategoryMap,
  actionMap,
  getCategoryTagType,
  getStatusTagType,
  canToggleStatus,
  canDelete,
  canRestore
} from '@/api/column'

export default {
  data() {
    return {
      loading: false,
      submitting: false,
      detailVisible: false,
      isFullscreen: false,
      reviewVisible: false,
      columnList: [],
      columnDetail: null,
      categoryList: [],
      total: 0,
      searchForm: {
        page: 1,
        pageSize: 10,
        category_id: '',
        keyWords: '',
        status: ''
      },
      reviewForm: {
        columnIds: [],
        status: 0,
        reviewMessage: ''
      },
      // 批量操作相关数据
      selectedColumns: [],
      batchReviewVisible: false,
      batchReviewForm: {
        columnIds: [],
        status: 0,
        reviewMessage: ''
      },
      submittingReview: false,
      tableKey: 0
    }
  },
  computed: {
    categoryOptions() {
      return createCategoryMap(this.categoryList)
    },
    statusOptions() {
      return statusMap
    },
    actionOptions() {
      return actionMap
    }
  },
  methods: {
    // 获取专栏列表
    async fetchColumnList() {
      this.loading = true
      try {
        const response = await getColumnList(this.searchForm)
        if (response.code === 1) {
          this.columnList = response.data.records || []
          this.total = response.data.total || 0
          this.tableKey++
        } else {
          this.$message.error(response.msg || '获取专栏列表失败')
        }
      } catch (error) {
        console.error('获取专栏列表失败:', error)
        this.$message.error('获取专栏列表失败')
      } finally {
        this.loading = false
      }
    },

    // 获取分类列表
    async fetchCategoryList() {
      try {
        const response = await getCategoryList()
        if (response.code === 1) {
          this.categoryList = response.data || []
        }
      } catch (error) {
        console.error('获取分类列表失败:', error)
      }
    },

    // 获取专栏详情
    async fetchColumnDetail(columnId) {
      try {
        const response = await getColumnDetail(columnId)
        if (response.code === 1) {
          this.columnDetail = response.data
          this.detailVisible = true
        } else {
          this.$message.error(response.msg || '获取专栏详情失败')
        }
      } catch (error) {
        console.error('获取专栏详情失败:', error)
        this.$message.error('获取专栏详情失败')
      }
    },

    // 搜索
    handleSearch() {
      this.searchForm.page = 1
      this.fetchColumnList()
    },

    // 重置搜索
    handleReset() {
      this.searchForm = {
        page: 1,
        pageSize: 10,
        category_id: '',
        keyWords: '',
        status: ''
      }
      this.fetchColumnList()
    },

    // 分页变化
    handleCurrentChange(page) {
      this.searchForm.page = page
      this.fetchColumnList()
    },

    // 页面大小变化
    handleSizeChange(size) {
      this.searchForm.pageSize = size
      this.searchForm.page = 1
      this.fetchColumnList()
    },

    // 查看详情
    viewDetail(row) {
      this.fetchColumnDetail(row.columnId)
    },

    // 关闭详情面板
    closeDetail() {
      this.detailVisible = false
      this.isFullscreen = false
      this.columnDetail = null
    },

    // 切换全屏
    toggleFullscreen() {
      this.isFullscreen = !this.isFullscreen
    },

    // 审核专栏
    handleReview(row) {
      this.reviewForm = {
        columnIds: [row.columnId],
        status: 0,
        reviewMessage: ''
      }
      this.reviewVisible = true
    },

    // 提交审核
    async submitReview() {
      if (!this.reviewForm.status) {
        this.$message.warning('请选择审核结果')
        return
      }

      this.submittingReview = true
      try {
        const response = await checkColumnStatus(this.reviewForm)
        if (response.code === 1) {
          this.$message.success('审核成功')
          this.reviewVisible = false
          this.fetchColumnList()
        } else {
          this.$message.error(response.msg || '审核失败')
        }
      } catch (error) {
        console.error('审核失败:', error)
        this.$message.error('审核失败')
      } finally {
        this.submittingReview = false
      }
    },

    // 批量审核
    handleBatchReview() {
      if (this.selectedColumns.length === 0) {
        this.$message.warning('请选择要审核的专栏')
        return
      }
      this.batchReviewForm = {
        columnIds: this.selectedColumns.map(item => item.columnId),
        status: 0,
        reviewMessage: ''
      }
      this.batchReviewVisible = true
    },

    // 提交批量审核
    async submitBatchReview() {
      if (!this.batchReviewForm.status) {
        this.$message.warning('请选择审核结果')
        return
      }

      this.submittingReview = true
      try {
        const response = await checkColumnStatus(this.batchReviewForm)
        if (response.code === 1) {
          this.$message.success('批量审核成功')
          this.batchReviewVisible = false
          this.selectedColumns = []
          this.fetchColumnList()
        } else {
          this.$message.error(response.msg || '批量审核失败')
        }
      } catch (error) {
        console.error('批量审核失败:', error)
        this.$message.error('批量审核失败')
      } finally {
        this.submittingReview = false
      }
    },

    // 启用专栏
    async handleEnable(row) {
      try {
        await this.$confirm('确定要启用该专栏吗？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })

        const response = await checkColumnStatus({
          columnIds: [row.columnId],
          status: 1,
          reviewMessage: ''
        })

        if (response.code === 1) {
          this.$message.success('启用成功')
          this.fetchColumnList()
        } else {
          this.$message.error(response.msg || '启用失败')
        }
      } catch (error) {
        if (error !== 'cancel') {
          console.error('启用失败:', error)
          this.$message.error('启用失败')
        }
      }
    },

    // 停用专栏
    async handleDisable(row) {
      try {
        await this.$confirm('确定要停用该专栏吗？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })

        const response = await checkColumnStatus({
          columnIds: [row.columnId],
          status: 2,
          reviewMessage: ''
        })

        if (response.code === 1) {
          this.$message.success('停用成功')
          this.fetchColumnList()
        } else {
          this.$message.error(response.msg || '停用失败')
        }
      } catch (error) {
        if (error !== 'cancel') {
          console.error('停用失败:', error)
          this.$message.error('停用失败')
        }
      }
    },

    // 删除专栏
    async handleDelete(row) {
      try {
        await this.$confirm('确定要删除该专栏吗？删除后可以恢复。', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })

        const response = await toggleColumnStatus(row.columnId, 'delete')

        if (response.code === 1) {
          this.$message.success('删除成功')
          this.fetchColumnList()
        } else {
          this.$message.error(response.msg || '删除失败')
        }
      } catch (error) {
        if (error !== 'cancel') {
          console.error('删除失败:', error)
          this.$message.error('删除失败')
        }
      }
    },

    // 恢复专栏
    async handleRestore(row) {
      try {
        await this.$confirm('确定要恢复该专栏吗？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })

        const response = await toggleColumnStatus(row.columnId, 'restore')

        if (response.code === 1) {
          this.$message.success('恢复成功')
          this.fetchColumnList()
        } else {
          this.$message.error(response.msg || '恢复失败')
        }
      } catch (error) {
        if (error !== 'cancel') {
          console.error('恢复失败:', error)
          this.$message.error('恢复失败')
        }
      }
    },

    // 表格选择变化
    handleSelectionChange(selection) {
      this.selectedColumns = selection
    },

    // 工具函数：获取分类标签
    getCategoryLabel(categoryId) {
      return this.categoryOptions[categoryId] || '未知分类'
    },

    // 工具函数：获取状态标签
    getStatusLabel(status) {
      return this.statusOptions[status] || '未知状态'
    },

    // 工具函数：获取操作标签
    getActionLabel(action) {
      return this.actionOptions[action] || '未知操作'
    },

    // 工具函数：格式化时间
    formatTime(time) {
      if (!time) return '-'
      return new Date(time).toLocaleString()
    },

    // 工具函数：格式化数字
    formatNumber(num) {
      if (num === null || num === undefined) return '-'
      return num.toLocaleString()
    },

    // 工具函数：获取分类标签类型
    getCategoryTagType(categoryId) {
      return getCategoryTagType(categoryId)
    },

    // 工具函数：获取状态标签类型
    getStatusTagType(status) {
      return getStatusTagType(status)
    },

    // 工具函数：检查是否可以启用/停用
    canToggleStatus(status) {
      return canToggleStatus(status)
    },

    // 工具函数：检查是否可以删除
    canDelete(status) {
      return canDelete(status)
    },

    // 工具函数：检查是否可以恢复
    canRestore(status) {
      return canRestore(status)
    }
  },
  mounted() {
    this.fetchCategoryList()
    this.fetchColumnList()
  }
}
